<?php
class CPyramidMemberBuy extends Zend_Db_Table
{
	protected $_name = '';
	protected $_primary = '';
	
	public function __construct()
	{
		$this->_name = R_PRE . 'pyramid_member_buy';
		$this->_primary = 'member_buy_id';
		parent::__construct();
	}
	
	public function add($row)
	{
		return $this->insert($row);
	}
	
	public function edit($row, $id)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('order_id = ?', $id);
		return $this->update($row, $where);
	}
	
	public function getMyBuyAmount($facebookId)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('facebook_id = ?', $facebookId);
		
		$sql = 'select sum(order_amount) as c from ' . $this->_name . ' where ' . $where . ' and is_pay = 1';
		$result = $db->fetchAll($sql);
	
		if (empty($result[0]['c'])) {
			return 0;
		}		
		return $result[0]['c'];
	}
	
	public function getCountBuyFriend($facebookId)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('from_facebook_id = ?', $facebookId);
		
		$sql = 'select count(*) as c from ' . $this->_name . ' where ' . $where . ' and is_pay = 1';
		$result = $db->fetchAll($sql);
		return $result[0]['c'];
	}
	
	public function getFriendBuyAmount($facebookId)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('from_facebook_id = ?', $facebookId);
		
		$sql = 'select sum(order_amount) as c from ' . $this->_name . ' where ' . $where . ' and is_pay = 1';
		$result = $db->fetchAll($sql);
		if (empty($result[0]['c'])) {
			return 0;
		}		
		return $result[0]['c'];
	}
}